<html>
<head>
	<meta http-equiv='Content-Type' content="text/html; charset=utf-8">
	<title>业魔殿--HHP3FES 合体模拟程序 Beta</title>
	<script language=javascript src=calv2.js></script>
	<script language=javascript src=index.js></script>
	<style>
		table{
  		background:#e1ebf5;
  		border:2px;
  		font-size:15px;
  		border-style:inset;
		}

	</style>
</head>

<body bgcolor=#e1ebf5>
<?php 
include('sql_class_point.php');
include('config.php');
include('fusion_function.php');
?>
<h1 align=center>请选择双方卡牌</h1>
<h3 align=center><strong>合体有风险，入市需谨慎</strong></h3>
<p>
<form action="" method=POST name="comm_form" id="comm_form">
<div align="center"><input type="submit" value="提交">&nbsp;&nbsp;<input type="reset" value="重设">
<input type=hidden name="mark">
</div>
<p>
<table align=center>

<tr>
	<td><table>
<tr>
	<td>合体对象(本体)</td>
  <td>
  <select name="pid[]" id="pida1" onChange="changelocation_dc(document.comm_form.pida1.options[document.comm_form.pida1.selectedIndex].value,document.comm_form.cida1);">
    <option selected value="">请选择牌组</option>
    <?php 
	$rs=$db->execute("select distinct pid,cgroup from cmember");
	while($rows=$db->fetch_array($rs))
	{
echo "<option value=".$rows["pid"].">".$rows["cgroup"]."</option>";
}
$db->free_result($rs);
?>
  </select>
  <script language="javascript">
subcat = new Array();
  <?php
  $rs=$db->execute("select * from cmember where available=1");
  $m=0;
  $k=0;
  
  while($rows=$db->fetch_array($rs)){
  $name1="'".$rows["pid"]."'";
  $name2="'".$rows["cmember"]."'";
  $name3="'".$rows["cid"]."'";
  ?>
subcat[<?php echo $m;?>] = new Array(<?php echo $name2;?>,<?php echo $name1;?>,<?php echo $name3;?>);
<?php
$m++;
$k++;//计算选项元素的个数
}
$db->free_result($rs);
 ?>
var twocount;
twocount=0;
twocount=<?php echo $k;?>;
</script>
  <select name="acid[]" id="cida1">
    <option selected value="">请选择牌名</option>
  </select>
</td>
</tr>
</table></td>
	<td><table>
<tr>
	<td>合体对象(素材)</td>
	<td>
  <select name="pid[]" id="pidb1" onChange="changelocation_dc(document.comm_form.pidb1.options[document.comm_form.pidb1.selectedIndex].value,document.comm_form.cidb1);">
    <option selected value="">请选择牌组</option>
    <?php 
	$rs=$db->execute("select distinct pid,cgroup from cmember");
	while($rows=$db->fetch_array($rs))
	{
echo "<option value=".$rows["pid"].">".$rows["cgroup"]."</option>";
}
$db->free_result($rs);
?>
  </select>
  <script language="javascript">
subcat = new Array();
  <?php
  $rs=$db->execute("select * from cmember where available=1");
  $m=0;
  $k=0;
  
  while($rows=$db->fetch_array($rs)){
  $name1="'".$rows["pid"]."'";
  $name2="'".$rows["cmember"]."'";
  $name3="'".$rows["cid"]."'";
  ?>
subcat[<?php echo $m;?>] = new Array(<?php echo $name2;?>,<?php echo $name1;?>,<?php echo $name3;?>);
<?php
$m++;
$k++;//计算选项元素的个数
}
$db->free_result($rs);
 ?>
var twocount;
twocount=0;
twocount=<?php echo $k;?>;
</script>
  <select name="bcid[]" id="cidb1">
    <option selected value="">请选择牌名</option>
  </select>
	</td>
</tr>
</table></td>
</tr>
<tr><td colspan=2><div align=center><input type=checkbox name=rfusion value=1>随机合体</div></td></tr>
</table>

<?
	get_terrain();
	$f_terrain = array(0=>"正常",1=>"新月",2=>"半月",3=>"月食",4=>"静天",5=>"满月",6=>"煌天");
?>

<p><table align=center>
<tr>
<td><div align=center>当前地形：<?echo $f_terrain[$_SESSION['FT']]?></div>
</td>
</tr>
</table><p>

</form>
<?

if (!isset($_POST[mark])) {
	exit;
}

class Char {}
class Skill {}

$aid=$_POST['acid'][0];
//随机合体判断
if($_POST[rfusion] == 1) {
	$bid=mt_rand(2,217);
	echo "开始随机合体...<p>";
} else {
	$bid=$_POST['bcid'][0];
}
$a=new Char;
$b=new Char;
$mskill=array();
$auskill=array();
$buskill=array();
$rs=$db->execute("select * from cmember where cid = ".$aid);
while($r=$db->fetch_array($rs)) {
	$a->cid = $r['cid'];
	$a->name = $r['cmember'];
	$a->skills = shuffle_string(trim($r['skills']));
}

$db->free_result($rs);
$rs=$db->execute("select * from cmember where cid = ".$bid);
while($r=$db->fetch_array($rs)) {
	$b->cid = $r['cid'];
	$b->name = $r['cmember'];
	$b->skills = shuffle_string(trim($r['skills']));
}

$rs=$db->execute("select * from skill where suid in ($a->skills)");
$i=0;
while ($r=$db->fetch_array($rs)) {
	$skill=new Skill;
	$skill->id=$r['suid'];
	$skill->owner="a";
	$skill->name=$r['sname'];
	$skill->desc=$r['sdesc'];
	$skill->level=$r['slevel'];
	$skill->unique=$r['isunique'];
	if($skill->unique != 1) {
		$mskill[$i] = $skill;
		$skill->position=$i;
		$i++;
	} else {
		$auskill[] = $skill;
	}
}
$db->free_result($rs);
$rs=$db->execute("select * from skill where suid in ($b->skills)");
while ($r=$db->fetch_array($rs)) {
	$skill=new Skill;
	$skill->id=$r['suid'];
	$skill->owner="b";
	$skill->name=$r['sname'];
	$skill->desc=$r['sdesc'];
	$skill->level=$r['slevel'];
	$skill->unique=$r['isunique'];
	if($skill->unique != 1) {
		$mskill[$i] = $skill;
		$skill->position=$i;
		$i++;
	} else {
		$buskill[] = $skill;
	}
}
$db->free_result($rs);
//echo "Num_total:".count($mskill)."<br>";

//设定需随机合成的技能个数
$askillnum=8-count($auskill);
$bskillnum=8-count($buskill);

$lv1=40;
$lv2=40;
$lv3=35;
$lv4=30;
$lv5=25;
$lv6=20;
$lv7=15;
$lv8=10;

$raskills=array();
$rbskills=array();
$cidarray=array();

for($i=0;$i<$askillnum;$i++) {
	//轮盘划分开始
	$basenum=0;
	foreach ($mskill as $s) {
		switch ($s->level) {
		case 1:{$s->mxfactor = $basenum+$lv1;$basenum += $lv1;$s->mnfactor=($s->mxfactor-$lv1+1);break;}
		case 2:{$s->mxfactor = $basenum+$lv2;$basenum += $lv2;$s->mnfactor=($s->mxfactor-$lv2+1);break;}
		case 3:{$s->mxfactor = $basenum+$lv3;$basenum += $lv3;$s->mnfactor=($s->mxfactor-$lv3+1);break;}
		case 4:{$s->mxfactor = $basenum+$lv4;$basenum += $lv4;$s->mnfactor=($s->mxfactor-$lv4+1);break;}
		case 5:{$s->mxfactor = $basenum+$lv5;$basenum += $lv5;$s->mnfactor=($s->mxfactor-$lv5+1);break;}
		case 6:{$s->mxfactor = $basenum+$lv6;$basenum += $lv6;$s->mnfactor=($s->mxfactor-$lv6+1);break;}
		case 7:{$s->mxfactor = $basenum+$lv7;$basenum += $lv7;$s->mnfactor=($s->mxfactor-$lv7+1);break;}
		case 8:{$s->mxfactor = $basenum+$lv8;$basenum += $lv8;$s->mnfactor=($s->mxfactor-$lv8+1);break;}
		default:{$s->mnfactor = $s->mxfactor = 1;}	//不应该出现
		}
	}

	//使用轮盘算法决定每次选出的技能存入raskills数组，并在mskill数组中删除被选中的元素
	$get_num=mt_rand(1,$basenum);
	foreach($mskill as $s) {
		if($get_num >= $s->mnfactor && $get_num <= $s->mxfactor) {
			$raskills[] = $s;
			unset($mskill[$s->position]);
			$mskill=array_values($mskill);
			for($k=0;$k<count($mskill);$k++) {
				$mskill[$k]->position=$k;
			}
			continue 2;
		}
	}
}

post_fusion($raskills);

//结果输出
echo $a->name."和".$b->name."的合体结果：<p>";
foreach($raskills as $ra) {
	echo $ra->name."&nbsp;".$ra->desc."<br>";
}
foreach($auskill as $au) {
	echo "<font color=#808000>".$au->name."</font>&nbsp;".$au->desc."<br>";
}

?>
<p><strong><font color=blue>注意：请手动在最终结果中删除可能出现的重复技能行。</font></strong>
</body>
</html>

